package observer;

import java.util.Hashtable;

import state.group.events.BaseEvent;
import state.group.events.ElevatorEvent;
import state.group.events.EventCollection;
import state.group.events.RegistrationEvent;

public class ElevatorDataCollection extends Hashtable<Byte, ElevatorPartialStruct> {
    
    private static final long serialVersionUID = -4216270671855456236L;

    public void updateStructsUsingEvents(EventCollection collection)
    {
        for (BaseEvent base : collection)
        {
            if (! (base instanceof ElevatorEvent ))
            {
                continue;
            }
            
            ElevatorEvent e = (ElevatorEvent) base;
            
            switch (e.getEventType())
            {
                case RegistrationEvent:
                    ElevatorPartialStruct newStruct = new ElevatorPartialStruct();
                    RegistrationEvent regEvent = (RegistrationEvent) e;
                    newStruct.setIdentifier(regEvent.getElevatorId());
                    newStruct.setDoorOpen(regEvent.isDoorOpen());
                    newStruct.setLastKnownFloor(regEvent.getFloor());
                    newStruct.setDirection(regEvent.getDirection());
                    
                    put(regEvent.getElevatorId(), newStruct);
                    break;
                case DeregistrationEvent:
                    this.remove(e.getElevatorId());
                    break;
                default:
                    
                    ElevatorPartialStruct relatedStruct = get(e.getElevatorId());
                    if (relatedStruct != null)
                    {
                        relatedStruct.updateStruct(e);
                    }
                    break;
            }
        }
    }
    
}
